Object Tracking with Optical Flow

In this lesson we'll learn:

  1. How to use Optical Flow in OpenCV
  2. Then use Dense Optical Flow

The Lucas-Kanade Optical Flow Algorithm

Optical flow is the pattern of apparent motion of image objects between two consecutive frames caused by the movemement of object or camera. It is 2D vector field where each vector is a displacement vector showing the movement of points from first frame to second. Consider the image below (Image Courtesy: Wikipedia article on Optical Flow).

Uzbek

Optik oqim - bu ob'ekt yoki kameraning harakati natijasida hosil bo'lgan ikkita ketma-ket kadrlar orasidagi tasvir ob'ektlarining ko'rinadigan harakati. Bu 2D vektor maydoni bo'lib, har bir vektor nuqtalarning birinchi kadrdan ikkinchisiga harakatini ko'rsatadigan siljish vektoridir. Ya'ni kadrdan kadrga o'tganda o'sha pixel boshqa joyga siljiydi. Yani siljish vektorlariga qarab harakatlanish

It shows a ball moving in 5 consecutive frames. The arrow shows its displacement vector. Optical flow has many applications in areas like :

Optical flow works on several assumptions:

Learn more - https://learnopencv.com/optical-flow-in-opencv/

2. Dense Optical Flow

Lucas-Kanade method computes optical flow for a sparse feature set (in our example, corners detected using Shi-Tomasi algorithm). OpenCV provides another algorithm to find the dense optical flow. It computes the optical flow for all the points in the frame. It is based on Gunner Farneback’s algorithm which is explained in “Two-Frame Motion Estimation Based on Polynomial Expansion” by Gunner Farneback in 2003.

Below sample shows how to find the dense optical flow using above algorithm. We get a 2-channel array with optical flow vectors, (u,v). We find their magnitude and direction. We color code the result for better visualization.